#include "WidgetVariantButton.h"
#include "DefineCommon.h"
#include "ui_WidgetVariantButton.h"
#include <iostream>
#include <WidgetsManager.h>
WidgetVariantButton::WidgetVariantButton(QWidget *parent) :
    WidgetVariant(parent),
    ui(new Ui::WidgetVariantButton)
{
    ui->setupUi(this);
}

WidgetVariantButton::~WidgetVariantButton()
{
    delete ui;
}

void WidgetVariantButton::refreshByProperty(EItemMetaPropertyPtr p)
{
    this->clearConnect();
    WidgetVariant::refreshByProperty(p);
    this->buildConnect();
}

void WidgetVariantButton::refreshByFunction(EItemMetaFunctionPtr p)
{
    this->clearConnect();
    this->m_funPtr = p;
    EString text = p->getName();
    if(text.left(3) == "ui_")
    {
        text.removeLeft(3);
    }
    ui->pushButton->setText(text.data());
    this->buildConnect();
}

void WidgetVariantButton::buildConnect()
{
	QObject::connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(slotButtonClicked(bool)));
}

void WidgetVariantButton::clearConnect()
{
	QObject::disconnect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(slotButtonClicked(bool)));
}

void WidgetVariantButton::slotButtonClicked(bool clicked)
{
    if(this->m_funPtr == nullptr)
    {
        ELOG_ERROR("this->m_funPtr == nullptr");
        return;
    }

    if(this->m_funPtr->getPropertyCount() == 0)
    {
        EVariant vReturn;
        this->m_funPtr->call(&vReturn);
        
    }

    //TODO: if this->m_funPtr has more than one property, 
    //a diaog should be constructed and show, 
    //User may fill property for this function

}
